<?php
require_once ('config.php');
require_once ('databasefunctions.php');
require_once ('displayfunctions.php');
validate_user();
html_header();

//Show menu

switch ($_GET['action']) {
	case "count" :
		save_search_list();

		// obtain name of table
		$program = $_SERVER['PHP_SELF'];
		foreach ($_SESSION[$program] as $index => $value) {
			if (substr($index,0,4) == 'from' || substr($index,0,2) == 'to') {
				$client = strstr($index, "ClientID");
				$job = strstr($index, "JobID");
				$company = strstr($index, "CompanyID");
				$project = strstr($index, "ProjectID");

				if ($client) $name = 'Client';
				if ($job) $name = 'Job';
				if ($company) $name = 'Company';
				if ($project) $name = 'Project';
				if (! empty($client) || ! empty($job) || ! empty($company) || ! empty($project)) break;
			}
		}
		$tablename =  strtolower($name) . 'master';
		$key = $name . 'ID';
		$from_key = 'from_' . $name . 'ID';
		$to_key = 'to_' . $name . 'ID';

		// select data from table
		$sql = "SELECT count(*) FROM $tablename WHERE 1";
		if (! empty($_GET[$from_key]))
			$sql .= " AND $key >= '" . $_GET[$from_key] . "'";
		if (! empty($_GET[$to_key]))
			$sql .= " AND $key <= '" . $_GET[$to_key] . "'";
		$sql_result = sql_execute($sql);
		$sql_data = mysql_fetch_array($sql_result);
		$no_of_record = $sql_data[0];

		$url_text = retrieve_search_list();
		?>
		<script>
		this.location = 'search.php?action=browse&total_record=<? echo $no_of_record ?>
						&record_offset=0<? echo $url_text ?>'
		</script>
		<?
		break;

	case "browse" :

		// obtain name of table
		$program = $_SERVER['PHP_SELF'];
		foreach ($_SESSION[$program] as $index => $value) {
			if (substr($index,0,4) == 'from' || substr($index,0,2) == 'to') {
				$client = strstr($index, "ClientID");
				$job = strstr($index, "JobID");
				$company = strstr($index, "CompanyID");
				$project = strstr($index, "ProjectID");

				if ($client) $name = 'Client';
				if ($job) $name = 'Job';
				if ($company) $name = 'Company';
                if ($project) $name = 'Project';
				if (! empty($client) || ! empty($job) || ! empty($company) || ! empty($project)) break;
			}
		}
		switch ($name) {
			case "Client" :
				$title = $langclientmaster;
				$langid = $langclientid;
				$langname = $langclientname;
				break;
			case "Job" :
				$title = $langjobmaster;
				$langid = $langjobid;
				$langname = $langjobname;
				break;
			case "Company" :
				$title = $langcompanymaster;
				$langid = $langcompanyid;
				$langname = $langcompanyname;
				break;
			case "Project" :
				$title = $langprojectmaster;
				$langid = $langprojectid;
				$langname = $langprojectname;
				break;
		}
		$tablename =  strtolower($name) . 'master';
		$key = $name . 'ID';
		$from_key = 'from_' . $name . 'ID';
		$to_key = 'to_' . $name . 'ID';
		$key_name = $name . 'Name';

		navigation_bar($title, 0, 0);
		?>
		<table>
			<tr bgcolor='<? echo $color1 ?>'>
				<td><? echo $langid ?>
				<td><? echo $langname ?>
				<td><? echo $langenablestatus ?>
		<?
		// List table records here
		$sql = "SELECT * FROM $tablename WHERE 1";
		if (! empty($_GET[$from_key]))
			$sql .= " AND $key >= '" . $_GET[$from_key] . "'";
		if (! empty($_GET[$to_key]))
			$sql .= " AND $key <= '" . $_GET[$to_key] . "'";
		$sql.= " ORDER BY $key LIMIT " . $_GET['record_offset'] . ", " . $_SESSION['RecordPerPage'];
		$sql_result = sql_execute($sql);

		while ($sql_data = mysql_fetch_array($sql_result)) {
			echo "\n<tr>";
			echo "<td>";
			if ($sql_data['Enable'] == 1) {
				echo "<a href='#' onclick=update_opener('". $sql_data[$key]."')>". $sql_data[$key] ."</a>";
			}
			else {
				echo $sql_data[$key];
			}
			echo "<td>";
			echo $sql_data[$key_name];
			echo "<td>";
			if ($sql_data['Enable'] == 1) echo $langyes;
			else echo $langno;
		}
		?>
		</table>
		<script>
		function update_opener(fieldvalue) {
			this.opener.document.<? echo $_SESSION['input_name'] ?>.value = fieldvalue;
			self.close();
		}
		</script>
		<?
		break;

	default :
		session_register('input_name');
		$_SESSION['input_name'] = $_GET['inputname'];
		switch ($_GET['master']) {
			case "client" :
				$title = $langclientmaster;
				$fieldname = 'ClientID';
				break;
			case "job" :
				$title = $langjobmaster;
				$fieldname = 'JobID';
				break;
			case "company" :
				$title = $langcompanymaster;
				$fieldname = 'CompanyID';
				break;
			case "project" :
				$title = $langprojectmaster;
				$fieldname = 'ProjectID';
				break;
		}

		$field[] = array('title'=>$title, 'field'=>$fieldname, 'length'=>10);
		search_form($field, $_SERVER['PHP_SELF']);
		break;
}

html_footer();
?>